package com.finalproject.ui;
/**
 * ExpenseProfile for UI
 */
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.finalproject.entity.CardHolder;
import com.finalproject.entity.CardHolderDatabase;
import com.finalproject.entity.CardHolderDatabase.CardHolderDatabaseObserver;
import com.finalproject.entity.ExpenseProfile;
import com.finalproject.entity.ExpenseProfile.ExpenseProfileObserver;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

public class UIExpenseProfile extends JPanel implements ExpenseProfileObserver, CardHolderDatabaseObserver{

	private JTextField addFundTextField = null;
	
	
	private JButton addFundsButton;
	
	
	private DecimalFormat priceFormat = new DecimalFormat("$0.00");
	
	public UIExpenseProfile(CardHolder  cardHolder) {
		super(new BorderLayout());
	
		CardHolderDatabase.getInstance().addCardHolderDatabaseObserver(this);
		
		setBorder(BorderFactory.createTitledBorder(""));
		
		
		
		
	}

	@Override
	public void onLogin(CardHolder cardHolder, Integer familyId) {
		
		loadExpenseProfile(cardHolder.getExpense());
		cardHolder.getExpense().addExpenseProfileObserver(this);
	}

	@Override
	public void onLogout(CardHolder cardHolder, Integer familyId) {
		removeAll();
		setBorder(BorderFactory.createTitledBorder(""));
		cardHolder = null;
		
	}

	@Override
	public void onUpdate(ExpenseProfile expenseProfile) {
		loadExpenseProfile(expenseProfile);
		
	}
	
	
	
	
	
	
	private void loadExpenseProfile(final ExpenseProfile expenseProfile) {
		removeAll();
		setBorder(BorderFactory.createTitledBorder("My Expense Profile"));
		
		DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout(""));
        builder.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        builder.appendColumn("left:pref");
        builder.appendColumn("1dlu");
        builder.appendColumn("fill:max(pref; 100px)");
        builder.appendColumn("1dlu");
        builder.appendColumn("left:pref");
        builder.appendColumn("1dlu");
        builder.appendColumn("fill:max(pref; 100px)");
       String fundsAvailable = UIHelper.currencyFormat.format(expenseProfile.getFundsAvailable());
       builder.append("My Available Funds:",new JLabel(fundsAvailable,JLabel.LEFT));
       builder.nextLine();
       
        addFundTextField = new JTextField();
		addFundsButton = new JButton("Add Funds");
		addFundsButton.setPreferredSize(new Dimension(250,50));
	   builder.append("Add Funds:", addFundTextField);
	   builder.nextLine();
        builder.append("",addFundsButton);
        
        
        addFundsButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
			
				addFunds(expenseProfile);
				
			}
		});
        add(builder.getPanel(),BorderLayout.CENTER);
	}
	
	
	private void addFunds(final ExpenseProfile expenseProfile) {
		String addFundsAvailableStr = addFundTextField.getText();
		try {
			double addFundsAvailable = Double.parseDouble(addFundsAvailableStr);
			
			expenseProfile.addFunds(addFundsAvailable);
			UIHelper.showConfirmMessage(this, "Added Funds sucessfully");
		} catch(Exception ex) {
			
			UIHelper.showErrorMessage(this, "Invalid Add Funds");
			ex.printStackTrace();


			
		}
	}
}
